﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
    <title>注册</title>
    <meta http-equiv='content-type' content='text/html;charset=utf-8'/>
    <script type='text/javascript' src='assets/js/jquery-1.8.2.min.js'></script>
    <script type='text/javascript'>
        $(document).ready(function() {
            var time = 60;
            var flag = true; //设置点击标记，防止60内再次点击生效

            //发送验证码
            $('#dyCodeButton').click(function () {
                $(this).attr("disabled", true);
                const emailValue = $(" input[ name='email' ] ").val();
                if (emailValue == null || emailValue === "") {
                    //alert("邮箱地址不能为空！");
                    const errorMsg = '<p><font color="red">邮箱地址不能为空！</font></p>';
                    $("#emailError").html(errorMsg);
                    return;
                }
                if (flag) {
                    var timer = setInterval(function () {
                        if (time === 60 && flag) {
                            flag = false;

                            $.ajax({
                                type: 'get',
                                async: false,
                                url: '/api/code/create/' + emailValue,
                                success: function (result) {
                                    if (result) {
                                        $("#dyCodeButton").html("已发送");
                                    } else {
                                        flag = true;
                                        time = 60;
                                        clearInterval(timer);
                                    }
                                }
                            });
                        } else if (time == 0) {
                            $("#dyCodeButton").removeAttr("disabled");
                            $("#dyCodeButton").html("免费获取验证码");
                            clearInterval(timer);
                            time = 60;
                            flag = true;
                        } else {
                            $("#dyCodeButton").html(time + " s 重新发送");
                            time--;
                        }
                    }, 1000);
                }

            });

            $(" input[ name='code' ] ").blur(function () {
                const codeStr = $(this).val();
                if (codeStr == null || codeStr === "") {
                    //alert("验证码不能为空！");
                    const errorMsg = '验证码不能为空!';
                    $("#errorMsg").html(errorMsg);
                    return;
                }
                const emailValue = $(" input[ name='email' ] ").val();
                if (emailValue == null || emailValue === "") {
                    //alert("邮箱地址不能为空！");
                    const errorMsg = '<p><font color="red">邮箱地址不能为空！</font></p>';
                    $("#emailError").html(errorMsg);
                    return;
                }

                // 验证code
                $.ajax({
                    type: 'get',
                    async: false,
                    url: '/api/code/validate/' + emailValue + '/' + codeStr,
                    success: function (result) {
                        if(result === 1){
                            const errorMsg = '<p><font color="red">验证码有误！</font></p>';
                            $("#errorMsg").html(errorMsg);
                            $(" input[ name='code' ] ").val("")
                        } else if(result === 2) {
                            const errorMsg = '<p><font color="red">验证码已失效！</font></p>';
                            $("#errorMsg").html(errorMsg);
                            $(" input[ name='code' ] ").val("")
                        } else {
                            //alert("验证码正确！");
                            $("#errorMsg").html("");
                        }
                    }
                });
            });
            $(" input[ name='email' ] ").blur(function () {
                const emailValue = $(" input[ name='email' ] ").val();
                if (emailValue == null || emailValue === "") {
                    //alert("邮箱地址不能为空！");
                    const errorMsg = '<p><font color="red">邮箱地址不能为空！</font></p>';
                    $("#emailError").html(errorMsg);
                    return;
                }
                // 验证邮箱是否已注册
                $.ajax({
                    type: 'get',
                    async: false,
                    url: '/api/user/isRegistered/' + emailValue,
                    success: function (result) {
                        if(result){
                            const errorMsg = '<p><font color="red">该邮箱已经被注册了！</font></p>';
                            $("#emailError").html(errorMsg);
                            $(" input[ name='email' ] ").val("");
                            isRegistered = true;
                        } else {
                            //alert("邮箱未被注册！");
                            $("#emailError").html("");
                        }
                    }
                });

            });


            $('#register').click(function () {
                const emailValue = $(" input[ name='email' ] ").val();
                if (emailValue == null || emailValue === "") {
                    //alert("邮箱地址不能为空！");
                    const errorMsg = '<p><font color="red">邮箱地址不能为空！</font></p>';
                    $("#emailError").html(errorMsg);
                    return;
                }

                var isRegistered = false;
                // 验证邮箱是否已注册
                $.ajax({
                    type: 'get',
                    async: false,
                    url: '/api/user/isRegistered/' + emailValue,
                    success: function (result) {
                        if(result){
                            const errorMsg = '<p><font color="red">该邮箱已经被注册了！</font></p>';
                            $("#errorMsg").html(errorMsg);
                            $(" input[ name='email' ] ").val("");
                            isRegistered = true;
                        } else {
                            //alert("邮箱未被注册！");
                            $("#errorMsg").html("");
                        }
                    }
                });
                if(isRegistered) return;

                const password = $(" input[ name='password' ] ").val();
                if (password == null || password === "") {
                    //alert("密码不能为空！");
                    const errorMsg = '<p><font color="red">密码不能为空！</font></p>';
                    $("#passwordError").html(errorMsg);
                    return;
                }

                const ConfirmPassword = $(" input[ name='ConfirmPassword' ] ").val();
                if (ConfirmPassword == null || ConfirmPassword === "") {
                    //alert("确认密码不能为空！");
                    const errorMsg = '<p><font color="red">确认密码不能为空！</font></p>';
                    $("#ConfirmPasswordError").html(errorMsg);
                    return;
                }

                if(ConfirmPassword != password){
                    const errorMsg = '<p><font color="red">两次密码不相同！</font></p>';
                    $("#ConfirmPasswordError").html(errorMsg);
                    $(" input[ name='ConfirmPassword' ] ").val("");
                    return;
                }

                const codeStr = $(this).val();
                if (codeStr == null || codeStr === "") {
                    //alert("验证码不能为空！");
                    const errorMsg = '<p><font color="red">验证码不能为空!</font></p>';
                    $("#errorMsg").html(errorMsg);
                    return;
                }

                const param = {'email':emailValue,'password':password, 'code':codeStr};
                //alert(JSON.stringify(param));
                $.ajax({
                    type:"post",
                    url:'/api/user/register',
                    dataType: 'json',
                    contentType:"application/json",
                    cache : false,
                    data: JSON.stringify(param),
                }).done(function(result){
                    //alert(JSON.stringify(result));
                    if(result.code === 200){
                        document.cookie = "sessionId=" + result.message;
                        window.location.href='welcome.html?email=' + emailValue;
                    }
                    else {
                        $('#errorMsg').html(result.message);
                    }
                }).fail(function(){
                    alert('fail');
                });
            });
            
        });
    </script>
    <style type='text/css'>
        #code {
            font-family: Arial, 宋体;
            font-style: italic;
            color: blue;
            size: 12px;
            border: 0;
            padding: 2px 3px;
            letter-spacing: 8px;
            font-weight: bolder;
        }
    </style>
</head>

<body>
<table border=0>
    <tr>
        <th width=200 height=30></th>
        <th><img src="images/register.jpg"></th>
    </tr>
</table>
<table border=0>
    <tr>
        <th width=700 height=400></th>
        <th>

            <table border=0 height=400>
                <tr>
                    <th height=65><font size='4'>邮 箱</font></th>
                    <th><input type="text" style="height:40px" placeholder="请输入邮箱账号" size=40 name="email"></th>
                    <th id="emailError"></th>
                </tr>
                <tr>
                    <th height=65><font size='4'>密 码</font></th>
                    <th><input type="password" style="height:40px" placeholder="密码长度为6~18字符" size=40 id="password"
                               name="password">
                    </th>
                    <th id="passwordError"></th>
                </tr>
                <tr>
                    <th height=65><font size='4'>确认密码</font></th>
                    <th><input type="password" style="height:40px" placeholder="请重新输入密码" size=40 id="ConfirmPassword"
                               name="ConfirmPassword">
                    </th>
                    <th id="ConfirmPasswordError"></th>
                </tr>
                <tr>
                    <th height=65><font size='4'>验证码</font></th>
                    <th><input type="text" style="height:40px" placeholder="请输入验证码" size=40 name="code"></th>
                    <th>
                        <a class="btn" href="javascript:void(0);" id="sendCode">
                            <span id="dyCodeButton">获取验证码</span>
                        </a>
                        <!--                        <button onclick="getCode()">获取验证码</button><p id="codeErrMsg"></p>-->
                    </th>
                </tr>


                <tr>
                    <th colspan=3 height=65><span id="errorMsg"></span></th>
                </tr>
                <tr>
                    <th colspan=2 height=65><input type='submit'
                                                   style="background-color:red;height:55px;width:160px;font-size:25px;color:white;border:none"
                                                   value='注册' id='register'></th>
                </tr>
            </table>

        </th>
    </tr>
</table>
<table>
    <tr>
        <th bgcolor='f9f8ed' height=200 width=2000><p>📧联系邮箱：xxxx@qq.com</p>
            <p>联系地址：XXXXX</p>
            <p>📞联系电话：XXXXX</p></th>
    </tr>
</table>
</body>
</body>
</html>
